import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] SUITS={"♠","♣","♦","♥"};
    public static List<Card> buyDeck(){
        //一副牌有52张牌
        List<Card> deck=new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <=13; j++) {
                String suits=SUITS[i];
                Card card=new Card();
                int rank1= j;
                card.rank=rank1;
                card.suit=suits;

                deck.add(card);
            }
        }
        return deck;
    }
    private static void swap(List<Card> deck, int i, int j) {
        Card t = deck.get(i);
        deck.set(i, deck.get(j));
        deck.set(j, t);
    }

    public static void shuffle(List<Card> deck){
        //生成一个伪随机数
        Random random=new Random(20241123);
        //i为51，下标为51即第52个数
        for (int i = deck.size()-1; i > 0; i--) {
            //r为生成的范围为范围为i内的数
            int r=random.nextInt(i);
            //交换第i个数和生成的随机数r
            swap(deck,r,i);
        }
    }
    public static void main(String[] args) {
        List<Card> deck = buyDeck();
        System.out.println("刚买回来的牌:");
        System.out.println(deck);
        shuffle(deck);
        System.out.println("洗过的牌:");
        System.out.println(deck);
        // 三个⼈，每个⼈轮流抓 5 张牌
        List<List<Card>> hands = new ArrayList<>();
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                hands.get(j).add(deck.remove(0));
            }
        }
        System.out.println("剩余的牌:");
        System.out.println(deck);
        System.out.println("A ⼿中的牌:");
        System.out.println(hands.get(0));
        System.out.println("B ⼿中的牌:");
        System.out.println(hands.get(1));
        System.out.println("C ⼿中的牌:");
        System.out.println(hands.get(2));
    }

}
